#!/usr/bin/python
# coding:utf-8

import time
import psutil
import urllib2
import threading


class RetrievingInformationByPsutil:
    def __init__(self, url = '127.0.0.1', port = 8000, frequency = 5):
        self.url_port = url + ":" + str(port)
        self.frequency = frequency

    def getCpuInfo(self):
        try:
            while True:
                cpudata = psutil.cpu_times()
                cpuPercent = psutil.cpu_percent(0)
                arguments = "?%s=%s&%s=%s&%s=%s&%s=%s&%s=%s"
                urlData = arguments%("userTime", cpudata.user, "systemTime", cpudata.system, "waitIo", cpudata.iowait, "idle",
                                     cpudata.idle, "cpuPercent", cpuPercent)
                url = "http://"+self.url_port+"/cgi-bin/getcpuinfo.py"+urlData
                print url
                print urllib2.urlopen(url).read()
                print '获取CPU信息成功'
                time.sleep(self.frequency)
        except Exception,e:
            print Exception,":",e

    def getMemoryInfo(self):
        try:
            while True:
                mem = psutil.virtual_memory()
                arguments = "?%s=%s&%s=%s&%s=%s&%s=%s"
                urlData = arguments%("total", mem.total, "used", mem.used, "free", mem.free, "percent", mem.percent)
                url = "http://"+self.url_port+"/cgi-bin/getmeminfo.py"+urlData
                print url
                print urllib2.urlopen(url).read()
                print '获取内存信息成功'
                time.sleep(self.frequency)
        except Exception,e:
            print Exception,":",e

    def getDisksInfo(self):
        try:
            while True:
                diskusage = psutil.disk_usage('/')
                arguments = "?%s=%s&%s=%s&%s=%s&%s=%s"
                urlData = arguments%("total", diskusage.total, "used", diskusage.used, "free", diskusage.free, "percent",
                                     diskusage.percent)
                url = "http://"+self.url_port+"/cgi-bin/getdiskinfo.py"+urlData
                print url
                print urllib2.urlopen(url).read()
                print '获取磁盘信息成功'
                time.sleep(self.frequency)
        except Exception,e:
            print Exception,":",e

    def getNetworkInfo(self):
        try:
            while True:
                netWork = psutil.net_io_counters()
                arguments = "?%s=%s&%s=%s&%s=%s&%s=%s"
                urlData = arguments%("bytes_sent", netWork.bytes_sent, "bytes_recv", netWork.bytes_recv, "packets_sent",
                                     netWork.packets_sent, "packets_recv", netWork.packets_recv)
                url = "http://"+self.url_port+"/cgi-bin/getnetinfo.py"+urlData
                print url
                print urllib2.urlopen(url).read()
                print '获取网络信息成功'
                time.sleep(self.frequency)
        except Exception,e:
            print Exception,":",e

    def getOtherSystemInfo(self):
        try:
            while True:
                loginusers = psutil.users()
                arguments = "?%s=%s&%s=%s&%s=%s"
                urlData = arguments%("name", loginusers[0].name, "host", loginusers[1].host, "logintime", loginusers[0].started)

                url = "http://"+self.url_port+"/cgi-bin/getotherinfo.py"+urlData
                print url
                print urllib2.urlopen(url).read()
                print '获取主机信息成功'
                time.sleep(self.frequency)
        except Exception,e:
            print Exception,":",e

ri = RetrievingInformationByPsutil('192.168.0.104')

threads = []

t1 = threading.Thread(target=ri.getCpuInfo)
threads.append(t1)
t2 = threading.Thread(target=ri.getMemoryInfo)
threads.append(t2)
t3 = threading.Thread(target=ri.getDisksInfo)
threads.append(t3)
t4 = threading.Thread(target=ri.getNetworkInfo)
threads.append(t4)
t5 = threading.Thread(target=ri.getOtherSystemInfo)
threads.append(t5)

if __name__ == '__main__':
    for t in threads:
        t.start()










